/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import pojo.Dimensionbookdata;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Roaa
 */
public class DimBookDataCrud {

    Connection con;
    PreparedStatement stmt;
    
    public DimBookDataCrud() {
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dimensionproject", "root", "root");
        } catch (InstantiationException ex) {
            Logger.getLogger(DimBookDataCrud.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(DimBookDataCrud.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DimBookDataCrud.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(DimBookDataCrud.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }

    public boolean update(Dimensionbookdata bData) {
        int f = 0;
        boolean result = false;
        try {
            stmt = con.prepareStatement("UPDATE dimensionbookdata SET length=?, length_by=?, width=?, width_by=?, depth=?, depth_by=?, weight=?, weight_by=?, pages=?, pages_by=?, format=?, format_by=?, mail_format=?, mail_format_by=?, items=?, items_by=?, rec_update_date=? WHERE ISBN= ?");
            stmt.setLong(18, bData.getIsbn());
            stmt.setInt(1, bData.getLength());
            stmt.setShort(2, bData.getLengthBy());
            stmt.setInt(3, bData.getWidth());
            stmt.setShort(4, bData.getWidthBy());
            stmt.setInt(5, bData.getDepth());
            stmt.setShort(6, bData.getDepthBy());
            stmt.setInt(7, bData.getWeight());
            stmt.setShort(8, bData.getWeightBy());
            stmt.setInt(9, bData.getPages());
            stmt.setShort(10, bData.getPagesBy());
            stmt.setString(11, bData.getFormat());
            stmt.setShort(12, bData.getFormatBy());
            stmt.setString(13, bData.getMailFormat());
            stmt.setShort(14, bData.getMailFormatBy());
            stmt.setInt(15, bData.getItems());
            stmt.setShort(16, bData.getItemsBy());
            System.out.println(bData.getLengthBy());
            stmt.setDate(17, new Date(bData.getRecUpdateDate().getTime()));
            f = stmt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(DimBookDataCrud.class.getName()).log(Level.SEVERE, null, ex);
        }
        result = (f==1)?true:false;
        return result;
    }

    public boolean insert(Dimensionbookdata bData) {
        int f = 0;
        boolean result = false;
        try {
            stmt = con.prepareStatement("INSERT INTO dimensionbookdata VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
            stmt.setLong(1, bData.getIsbn());
            stmt.setInt(2, bData.getLength());
            stmt.setShort(3, bData.getLengthBy());
            stmt.setInt(4, bData.getWidth());
            stmt.setShort(5, bData.getWidthBy());
            stmt.setInt(6, bData.getDepth());
            stmt.setShort(7, bData.getDepthBy());
            stmt.setInt(8, bData.getWeight());
            stmt.setShort(9, bData.getWeightBy());
            stmt.setInt(10, bData.getPages());
            stmt.setShort(11, bData.getPagesBy());
            stmt.setString(12, bData.getFormat());
            stmt.setShort(13, bData.getFormatBy());
            stmt.setString(14, bData.getMailFormat());
            stmt.setShort(15, bData.getMailFormatBy());
            stmt.setInt(16, bData.getItems());
            stmt.setShort(17, bData.getItemsBy());
            System.out.println(bData.getLengthBy());
            stmt.setDate(18, new Date(bData.getRecUpdateDate().getTime()));
            f = stmt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(DimBookDataCrud.class.getName()).log(Level.SEVERE, null, ex);
        }
        result = (f==1)?true:false;
        return result;
    }

    public boolean delete(long isbn) {
        int f = 0;
        boolean result = false;
        try {
            stmt = con.prepareStatement("DELETE FROM dimensionbookdata WHERE ISBN=?");
            stmt.setLong(1, isbn);
            f = stmt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(DimBookDataCrud.class.getName()).log(Level.SEVERE, null, ex);
        }
        result = (f==1)?true:false;
        return result;
    }

}
